<!--
 Copyright 2025 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<mat-expansion-panel>
  <mat-expansion-panel-header class="panel-heading-container">
    <div class="configure-title">
      <span>{{ getHumanReadablestring(this.intent.name) }}</span>
      <button
        mat-button
        class="inactive-button"
        disabled="true"
        *ngIf="this.intent.status !== '5'"
      >
        <span>In progress</span>
      </button>
      <button
        mat-button
        class="active-button"
        disabled="true"
        *ngIf="this.intent.status == '5'"
      >
        <span>Active</span>
      </button>
    </div>
    <div class="intent-buttons-container">
      <button
        mat-icon-button
        class="reset-button"
        *ngIf="!this.editMode"
        (click)="toggleEditMode()"
      >
        <mat-icon>edit</mat-icon>
      </button>
      <button
        mat-icon-button
        class="reset-button"
        *ngIf="!this.editMode"
        (click)="showDeleteDialog($event)"
      >
        <mat-icon>delete</mat-icon>
      </button>
      <button
        mat-raised-button
        class="reset-button"
        *ngIf="this.editMode"
        (click)="toggleEditMode()"
      >
        <span> Discard Changes </span>
      </button>
      <button
        mat-raised-button
        class="reset-button"
        *ngIf="this.editMode"
        (click)="saveForm()"
      >
        <span>Save</span>
      </button>
      <div class="spinner-container" *ngIf="this.showSpinner">
        <mat-spinner style="width: 60px; height: 60px"></mat-spinner>
      </div>
    </div>
  </mat-expansion-panel-header>

  <div [formGroup]="this.intentForm">
    <div class="input-field-container">
      <mat-stepper class="stepper" *ngIf="this.intent.status !== '5'">
        <mat-step [completed]="this.intent.status >= '1'" [editable]="false">
          <ng-template matStepLabel>Index endpoint created</ng-template>
        </mat-step>
        <mat-step
          [errorMessage]="
            'There was a problem creating an index with your data'
          "
          [hasError]="this.intent.status == '2'"
          [completed]="this.intent.status >= '3'"
          [editable]="false"
        >
          <ng-template matStepLabel>Index created</ng-template>
        </mat-step>
        <mat-step
          [errorMessage]="'There was a problem deploying your index'"
          [hasError]="this.intent.status == '4'"
          [completed]="this.intent.status >= '5'"
          [editable]="false"
        >
          <ng-template matStepLabel>Index deployed</ng-template>
        </mat-step>
      </mat-stepper>
    </div>
    <div class="input-field-container">
      <mat-form-field
        class="intent-field"
        appearance="outline"
        [floatLabel]="'always'"
      >
        <mat-label>Name</mat-label>
        <input
          matInput
          [formControl]="this.intentForm.controls.name"
          placeholder="Intent name"
          type="text"
        />
      </mat-form-field>
      <mat-form-field
        *ngIf="this.intent.gcp_bucket"
        class="intent-field"
        appearance="outline"
        [floatLabel]="'always'"
      >
        <mat-label>GCS Path to files</mat-label>
        <input
          matInput
          [formControl]="this.intentForm.controls.gcp_bucket"
          placeholder="gs://your-bucket/your-folder"
          type="text"
        />
      </mat-form-field>
    </div>
    <div class="input-field-container">
      <mat-form-field
        class="intent-field"
        appearance="outline"
        [floatLabel]="'always'"
      >
        <mat-label>AI Model</mat-label>
        <mat-select
          [formControl]="this.intentForm.controls.ai_model"
          placeholder="AI Model"
        >
          <mat-optgroup *ngFor="let model of this.models" [label]="model.name">
            <mat-option
              *ngFor="let model_name of model.models"
              [value]="model_name"
              >{{ model_name }}</mat-option
            >
          </mat-optgroup>
        </mat-select>
      </mat-form-field>
      <div class="ai-temperature-field">
        <div class="ai-temperature-label-container">
          <label>Creativity</label>
          <label>{{ this.intentForm.controls.ai_temperature.value }}</label>
        </div>
        <mat-slider
          style="width: 100%"
          [max]="2"
          [min]="0"
          [step]="0.1"
          [discrete]="true"
          [showTickMarks]="true"
        >
          <input
            matSliderThumb
            [formControl]="this.intentForm.controls.ai_temperature"
          />
        </mat-slider>
      </div>
    </div>
    <div class="input-field-container">
      <mat-form-field
        class="prompt-field"
        appearance="outline"
        [floatLabel]="'always'"
      >
        <mat-label>Description</mat-label>
        <input
          matInput
          [formControl]="this.intentForm.controls.description"
          placeholder="Enter a short description"
          type="text"
        />
      </mat-form-field>
    </div>
    <div class="input-field-container">
      <mat-form-field
        class="prompt-field"
        appearance="outline"
        [floatLabel]="'always'"
      >
        <mat-label>Prompt</mat-label>
        <textarea
          matInput
          [formControl]="this.intentForm.controls.prompt"
          placeholder="Enter Prompt here"
          rows="3"
          wrap="soft"
        >
        </textarea>
      </mat-form-field>
    </div>
    <mat-list>
      <mat-list-item
        class="questions-field-container"
        style="height: 100%"
        *ngFor="
          let control of this.intentForm.controls.questions.controls;
          let i = index
        "
      >
        <mat-form-field
          class="search-question-field"
          appearance="outline"
          [floatLabel]="'always'"
        >
          <input
            matInput
            placeholder="Enter a Question"
            [formControl]="control"
            type="text"
          />
          <mat-icon *ngIf="this.editMode" matSuffix (click)="removeQuestion(i)">delete</mat-icon>
        </mat-form-field>
      </mat-list-item>
      <button
        *ngIf="this.editMode"
        mat-button
        class="add-question-button"
        (click)="addQuestion()"
      >
        Add Question
      </button>
    </mat-list>
  </div>
</mat-expansion-panel>

<ng-template #deleteDialogRef let-data>
  <div>
    <mat-dialog-content>
      <div class="image-container">
        <img
          src="../../../../assets/images/delete_notification_dialog_image.png"
        />
      </div>
      <span class="delete-confirmation-text"
        >Are you sure you wanna delete this Intent?</span
      >
    </mat-dialog-content>
    <mat-dialog-actions>
      <div
        class="dialog-actions-container"
      >
        <button mat-raised-button class="discard-button" mat-dialog-close>
          Cancel
        </button>
        <button
          mat-raised-button
          class="dialog-delete-button"
          (click)="deleteIntent()"
        >
          Delete
        </button>
      </div>
    </mat-dialog-actions>
  </div>
</ng-template>
